let logger = require('pomelo-logger').getLogger(__filename);
let DateUtil = require("../../../utils/dateUtil");

module.exports = function () {
    return new Filter();
};

let Filter = function () {

};

Filter.prototype.before = function (msg, session, next) {
    session.__startTime__ = DateUtil.nowMS();
    next(null);
};

Filter.prototype.after = function (err, msg, session, resp, next) {
    let start = session.__startTime__;
    if (typeof start === 'number') {
        let timeUsed = DateUtil.nowMS() - start;
        let log = {
            route: msg.__route__,
            args: msg,
            response: resp,
            time: new Date(start),
            timeUsed: timeUsed
        };
        logger.info(JSON.stringify(log));
    }
    next(err);
};